<template>
  <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in employees" :key="item.id" :value="item.username" :label="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getSimpleEmployees } from '@/api/employees'
import { addNewDepartment, editDetailById, putDetailById } from '@/api/department'
export default {
  props: {
    id: {
      type: String,
      // default: ''
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    const checkCode = (rules, value, callback) => {
      // const flag = this.originList.some(item => item.code === value)
      let codeList = null
      if (this.isEdit) {
        // 筛选出所有id不等于点击时id的对象， 然后拿到里面的code
        codeList = this.originList.filter(item => item.id !== this.id).map(item => item.code)
        // flag ? callback(new Error(value + '已存在')) : callback()
      } else {
        codeList = this.originList.map(item => item.code)
      }
      codeList.includes(value) ? callback(new Error(value + '已存在')) : callback()
    }
    const checkName = (rules, value, callback) => {
      let nameList = null
      // 1.如果打开的是编辑框
      if (this.isEdit) {
        // 找到点击的那个部门的id对象 然后拿到该对象的pid
        const pid = this.originList.find(item => item.id === this.id).pid
        // 因为同级部门的pid都相同 所以筛选出item中拥有相同的pid的对象(除了本身) 然后再返回出这些对象里的name属性
        nameList = this.originList.filter(item => item.pid === pid && item.id !== this.id).map(item => item.name)
      } else {
        // 2.如果打开的是添加框
        // 筛选出同级目录中所有选项的名字
        nameList = this.originList.filter(item => item.pid === this.id).map(item => item.name)
      }
      nameList.includes(value) ? callback(new Error(value + '已存在')) : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 2, max: 10, message: '请输入2-10个字符', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { validator: checkCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请选做部门负责人名称', trigger: 'change' },
          { min: 2, max: 10, message: '请输入2-10个字符', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '请输入部门简介', trigger: 'blur' },
          { min: 2, max: 10, message: '请输入2-10个字符', trigger: 'blur' }
        ]
      },
      employees: []
    }
  },
  created() {
    this.loadEmployees()
    this.loadDtail()
  },
  methods: {
    // 编辑弹框内容
    async loadDtail() {
      // 趁早返回
      if (!this.isEdit) return
      // 发送获取部门详情请求
      const res = await editDetailById(this.id)
      // console.log(res)
      // 将部门详情信息放入form里
      this.form = res.data
    },
    async hSubmit() {
      this.$refs.deptForm.validate(async valid => {
        if (!valid) return
        try {
          let res = null
          // 如果打开的编辑框则发送的是修改部门详情的请求
          this.isEdit ? res = await putDetailById(this.form) : res = await addNewDepartment({ ...this.form, pid: this.id })
          this.$message.success(res.message)
          // 子向父传值自定一个事件 然后再父组件里关闭弹窗 重新渲染页面
          this.$emit('success')
        } catch (e) {
          this.$message.error(e.message)
        }
      })
    },
    hCancel() {
      this.$emit('close')
    },
    // 获取dialog输入框中的人员信息
    async loadEmployees() {
      try {
        const res = await getSimpleEmployees()
        // console.log(res)
        this.employees = res.data
      } catch (e) {
        console.log(e)
      }
    }
  }
}
</script>
